package com.qfedu.yanglao20admin.refund.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.enuminfo.StateEnum;
import com.qfedu.yanglao20.entity.refund.CancelConfiguration;
import com.qfedu.yanglao20admin.refund.dao.CancelConfigurationDao;
import com.qfedu.yanglao20admin.refund.param.CancelConfigurationParam;
import com.qfedu.yanglao20admin.refund.service.CancelConfigurationService;
import com.qfedu.yanglao20admin.refund.vo.CancelConfigurationVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/5/5 17:26
 */
@Service
public class CancelConfigurationServiceImpl implements CancelConfigurationService {

    @Resource
    private CancelConfigurationDao cancelConfigurationDao;

    @Override
    public Page<CancelConfigurationVO> pageCancelConfiguration(CancelConfigurationParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<CancelConfigurationVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<CancelConfigurationVO> cancelConfigurationVOS = cancelConfigurationDao.selectList(param);

        //  循环遍历
        for (CancelConfigurationVO vo :
                cancelConfigurationVOS) {
            StateEnum stateById = StateEnum.getStateById(vo.getEnableFlag());
            vo.setStateDesc(stateById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = cancelConfigurationDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(cancelConfigurationVOS);

        return page;
    }

    @Override
    public void deleteById(Integer id) {
        cancelConfigurationDao.deleteById(id);
    }

    @Override
    public void addCancelConfiguration(CancelConfiguration cancelConfiguration) {
        cancelConfigurationDao.insert(cancelConfiguration);
    }

    @Override
    public CancelConfiguration queryCancelConfiguration(Integer id) {
        return cancelConfigurationDao.selectById(id);
    }

    @Override
    public void updateCancelConfiguration(CancelConfiguration cancelConfiguration) {
        cancelConfigurationDao.updateById(cancelConfiguration);
    }
}
